import axios from 'axios'
import {ElMessage} from "element-plus";
import useUserStore from "@/store/modules/user.ts";

const request = axios.create({
  baseURL: import.meta.env.VITE_SERVE,
  timeout: 5000,
});

request.interceptors.request.use(
  (config) => {
    const userStore = useUserStore();
    if (userStore.token !== '') {
      config.headers.Authorization = "Bearer " + userStore.token;
    }
    return config;
  }
);

request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    ElMessage.error(error.message);
    return Promise.reject(error);
  }
);

export default request;
